using System;
namespace Pine.Core
{
	public sealed class CharValue : Value, IEquatable<CharValue>
	{
		public char Value { get; private set; }
		
		private Function ToStringWrapper;
		
		public CharValue(char value) {
			Value = value;
			
			ToStringWrapper = new Function(null, ToStringAdapted);
		}
		
		public void ToStringAdapted(Function f, ValueList rl) {
			if (rl == null)
				throw new ArgumentNullException("rl", "Argument list of function can not be null");
			
			rl.Clear();
			rl.SetArrayElement(new Number(0), new StringValue(ToString()));
		}
		
		public override string ToString ()
		{
			return Value.ToString();
		}
		
		public override bool Equals (object obj)
		{
			return Equals(obj as CharValue);
		}
		
		public bool Equals(CharValue obj) {
			return obj != null && Value == obj.Value;
		}
		
		public override Value FieldGet (string fieldname)
		{
			if (fieldname == "toString")
				return ToStringWrapper;
			throw new PineException("No such field in char: " + fieldname);
		}
		
		public override int GetHashCode ()
		{
			return Value.GetHashCode();
		}
	}
}

